package com.yunhe.sms.controller.system;

import com.yunhe.commons.dto.Pageable;
import com.yunhe.sms.entity.system.Role;
import com.yunhe.sms.entity.system.User;
import com.yunhe.sms.service.system.RoleService;
import com.yunhe.sms.service.system.UserService;
import com.yunhe.sms.service.system.impl.RoleServiceImpl;
import com.yunhe.sms.service.system.impl.UserServiceImpl;
import com.yunhe.webmvc.annotation.RequestBean;
import com.yunhe.webmvc.annotation.RequestMapping;
import com.yunhe.webmvc.annotation.ResponseJson;
import com.yunhe.webmvc.model.JSONResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/10/12 0012.
 */
@RequestMapping("/user")
public class UserController {
    private UserService userService = new UserServiceImpl();
    private RoleService roleService = new RoleServiceImpl();

    /**
     * 加载用户列表页
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping
    public String list(){
        return "user/list";
    }

    /**
     * 加载用户添加页
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/add")
    public String add(HttpServletRequest req){
        List<Role> roleList = roleService.findAllUsable();
        req.setAttribute("roleList",roleList);
        return "user/add";
    }

    /**
     * 加载用户编辑页
     * @param req
     */
    @RequestMapping("/edit")
    public String edit(HttpServletRequest req, Long id){

        User user = userService.findOne(id);
        List<Role> roleList = roleService.findAllUsable();

        req.setAttribute("user",user);
        req.setAttribute("roleList",roleList);

        return "user/edit";

    }

    /**
     * 删除用户方法
     */
    @RequestMapping("/delete")
    public String del(Long id){
        userService.delete(id);
        return "redirect:/user";
    }

    /**
     * 批量删除用户方法
     * @throws IOException
     */
    @RequestMapping("/delBatch")
    @ResponseJson
    public JSONResponse delBatch(String ids){
        userService.delBatch(ids);
        return JSONResponse.success("删除成功");
    }

    /**
     * 添加用户方法
     * @throws IOException
     */

    @RequestMapping("/save")
    public String save(@RequestBean User user){
        if(null != user){
            userService.save(user);
        }
        return "redirect:/user";
    }


    /**
     * 修改用户信息
     * @param user
     * @return
     * @throws SQLException
     */
    @RequestMapping("/update")
    public String update( @RequestBean User user){
        if(null != user){
            userService.update(user);
        }
        return "redirect:/user";
    }


    @RequestMapping("/listData")
    @ResponseJson
    public Pageable listData(Integer offset, Integer limit){
        return userService.findPage(offset, limit);
    }


    @RequestMapping("/onlyOne")
    @ResponseJson
    public JSONResponse onlyOne(String userName){
        Boolean flag = userService.findOne(userName);
        return JSONResponse.success("校验成功",flag);

    }

    @RequestMapping("/onlyOne2")
    @ResponseJson
    public String onlyOne2(String userName){
        Boolean flag = userService.findOne(userName);
        return flag.toString();
    }


    @RequestMapping("/sendEmail")
    @ResponseJson
    public JSONResponse sendEmail(@RequestBean User user){
        String msg = userService.sendEmail(user);
        return JSONResponse.success(msg);
    }


    @RequestMapping("/resetPass")
    @ResponseJson
    public JSONResponse mmpd(HttpServletRequest req, String pwd, String newPwd, String newPwd2){
        User user = (User) req.getSession().getAttribute("user");
        String message = "";
        if(pwd.equals(user.getPassword())){
            if(newPwd.equals(newPwd2)){
                if(!newPwd.equals(user.getPassword())){
                    userService.updatePwd(newPwd,user.getId());
                    return JSONResponse.success("密码修改成功");
                }else{
                    message = "新密码与原密码相同";
                }
            }else{
                message = "两次密码不一致";
            }
        }else{
            message = "用户密码输入错误";
        }
        return JSONResponse.fail("",message);

    }

}
